//
//  WithdrawListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/8.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "WithdrawSectionHeader.h"
#import "WithdrawListCell.h"

#import "WithdrawListViewController.h"

@interface WithdrawListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <CommonWithDrawListModel *>* dataArray;
@end

@implementation WithdrawListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_withDrawList];
}


- (void)initView {
    
    self.title = @"提现明细";
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

- (void)goDetail {
    WithdrawListViewController *vc = [[WithdrawListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    CGFloat height = indexPath.section/2==0?90:180;
    
    return self.dataArray[indexPath.section].itemHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    WithdrawSectionHeader *headView = [[NSBundle mainBundle] loadNibNamed:@"WithdrawSectionHeader"owner:nil options:nil].firstObject;
    headView.statusLab.text = self.dataArray[section].wStatusStr;
    return  headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WithdrawListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.section]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    WithdrawViewController *vc = [[WithdrawViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"WithdrawListCell" bundle:nil] forCellReuseIdentifier:@"WithdrawListCellID"];
    }
    return _tableview;
}

- (NSMutableArray<CommonWithDrawListModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_withDrawList {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_WithDrawList];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSDictionary *t = @{}.mutableCopy;
    [t setValue:[TTUserModel sharedObject].userId forKey:@"wbaseId"];
    [dic setValue:t forKey:@"t"];
    [dic setValue:@(1) forKey:@"pageNum"];
    [dic setValue:@(20) forKey:@"pageSize"];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];

                if ([list isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        if ([listDic isKindOfClass:[NSDictionary class]]) {
                            CommonWithDrawListModel *model = [CommonWithDrawListModel modelWithDictionary:list[i]];
                            
                            //1：已申请待审核，2：审核通过，3：审核拒绝，4：准备提现(不用)，5：提现成功，6：提现失败
                            switch (model.wstatus) {
                                case 1:{
                                    model.wStatusStr = @"已申请待审核";
                                    model.itemHeight = 90;
                                } break;
                                case 2:{
                                    model.wStatusStr = @"审核通过";
                                    model.itemHeight = 90;
                                } break;
                                case 3:{
                                    model.wStatusStr = @"审核拒绝";
                                    model.itemHeight = 180;

                                } break;
                                case 4:{
                                    model.wStatusStr = @"";
                                    model.itemHeight = 90;

                                } break;
                                case 5:{
                                    model.wStatusStr = @"提现成功";
                                    model.itemHeight = 90;

                                } break;
                                case 6:{
                                    model.wStatusStr = @"提现失败";
                                    model.itemHeight = 180;

                                } break;
                                default:{
                                    model.wStatusStr = @"未获取到状态";
                                    model.itemHeight = 90;

                                } break;
                            }
                            
                            [weakSelf.dataArray addObject:model];
                        }
                    }
                    [weakSelf.tableview reloadData];
                }
            }
        }

        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
